<?php
	include_once "../classes/module_bs.php";
	include_once "../classes/thuvienham.php";
	if(!IsAdmin())
		header("Location: index.php");
	$MaModule = $_GET['id'];
	$PIN      = $_GET['pin'];
	
	$module = new module_bs();
	$module->SetMaModule($MaModule);
	$module_data = $module->LayDanhSach();
	
	if($module_data[0]['MaModule'] == $MaModule && $module_data[0]['PIN'] == $PIN)
	{	
		$module->SetPIN(GeneratePIN());
		$module->CapNhatPIN();
		
		$module = new module_bs();
		$module_data = $module->LayDanhSach();
		for($i = count($module_data) - 1; $i > 0; $i--)
		{
			if($module_data[$i - 1]['MaModule'] == $MaModule)
			{
				$m1 = $module_data[$i]['MaModule'];
				$ind1 = $module_data[$i]['Index'];
				
				$m2 = $module_data[$i - 1]['MaModule'];
				$ind2 = $module_data[$i - 1]['Index'];
				
				break;
			}
		}
		
		$module = new module_bs();
		$module->SetMaModule($m1);
		$module->SetIndex($ind2);
		$kq1 = $module->CapNhatIndex();
		
		$module = new module_bs();
		$module->SetMaModule($m2);
		$module->SetIndex($ind1);
		$kq2 = $module->CapNhatIndex();
	}
	else
	{
		$module->SetPIN(GeneratePIN());
		$module->CapNhatPIN();
	}
	
	QuayVeTrangTruoc();
?>